const baseUrl = 'https://box.jiezhui.top';    
// const baseUrl = 'http://www.news2.xin';    

// 定义基础请求路径(后端服务器地址)
export function baseRequest(url, data){

		//加载中
		if(ShowLoadingUrl.indexOf(url)>=0){
			uni.showLoading({
				icon:'none',
				title: '加载中'
			});
		}

		
		if(uni.getStorageSync('token')){
			if(data){
				data.token = uni.getStorageSync('token')
			}else{
				data = {token:uni.getStorageSync('token')}
			}
			
		}
		let baseDefaultOpts = {
			url: baseUrl+url+'?appid='+uni.getAccountInfoSync().miniProgram.appId+'&fappid='+uni.getStorageSync('fappid'),
			// 请求接口地址
			data: data,
			method: 'post',
			// 配置请求类型
			// header: opts.method == 'get' ? {'X-Requested-With': 'XMLHttpRequest',"Accept": "application/json","Content-Type": "application/json; charset=UTF-8"} : {'X-Requested-With': 'XMLHttpRequest','Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
			// header: {'token': uni.getStorageSync('token')?uni.getStorageSync('token'):''},
			
			// 配置请求头
			dataType: 'json',
			}
			let promise = new Promise(function(resolve, reject) {
				uni.request(baseDefaultOpts).then(
					(res) => {
						
						if(ShowLoadingUrl.indexOf(url)>=0){
							setTimeout(function () {
							  uni.hideLoading();
							}, 200);
						}
					
						
						if(res[1].data.code==1001){
							uni.setStorageSync('token','')
							uni.setStorageSync('userinfo','')
							uni.setStorageSync('is_login',false)
							uni.showToast({
								title: '登录已过期',
								icon: 'none',
								duration: 1500
							});
							setTimeout(function(){
								uni.navigateTo({
									url: '/pages/login/login',
								});
							},1500)
					
							return
						}
						
						resolve(res[1].data)
					}
				).catch(
					(response) => {
						reject(response)
					}
				)
			})
			return promise	
};

const ShowLoadingUrl = [
	'/wxapp/zhuangyuan/zyGetRankList.html',
	'/wxapp/zhuangyuan/xyFollowList.html',
	'/wxapp/tool/getRecommendXcx.html'
]

const TokenUrl = [
	//user
	'/wxapp/user/logout.html',
	//like
	'/wxapp/like/getLikeList.html',
	'/wxapp/like/like.html',
	//danmu
	'/wxapp/KapaiDanmu/saveDanmu.html',
	//power
	'/wxapp/power/signInit.html',
	//kapai
	'/wxapp/Kapai/chouKa.html',
	//ukapai
	'/wxapp/Ukapai/getUkapaiDayCount.html',
	'/wxapp/Ukapai/getUkapaiList.html',
	'/wxapp/Ukapai/duihuan.html',
	//庄园
	'/wxapp/zhuangyuan/index.html',
	'/wxapp/zhuangyuan/attack.html',
	'/wxapp/zhuangyuan/zyGetRankList.html',
	'/wxapp/zhuangyuan/xyFollowList.html',
	
]


